package com.hc.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hc.mall.product.entity.AttrEntity;
import com.hc.mall.product.service.AttrService;
import com.hc.mall.product.vo.AttrGroupWithAttrsVo;
import com.hc.mall.product.vo.SkuItemAttrGroupVo;
import com.hc.mall.product.vo.SkuItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.common.utils.PageUtils;
import com.hc.common.utils.Query;

import com.hc.mall.product.dao.AttrGroupDao;
import com.hc.mall.product.entity.AttrGroupEntity;
import com.hc.mall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        /* select * from pms_attr_group where catelog_id=catelogId
            and (attr_group_id=key or attr_group_name like "%key% ") */
        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>().lambda();
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((obj)->{
                obj.eq(AttrGroupEntity::getAttrGroupId,key)
                        .or().like(AttrGroupEntity::getAttrGroupName,key);
            });
        }
        if(catelogId == 0){
           return new PageUtils(this.page(new Query<AttrGroupEntity>().getPage(params),queryWrapper));
        }else{
            queryWrapper.eq(AttrGroupEntity::getCatelogId,catelogId);
            return new PageUtils(this.page(new Query<AttrGroupEntity>().getPage(params),queryWrapper));
        }
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrs(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId));
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupEntities.stream().map(item -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item, attrGroupWithAttrsVo);
            List<AttrEntity> attrEntities = attrService.getRelationAttr(item.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrEntities);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return attrGroupWithAttrsVos;
    }

    @Override
    public List<SkuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        return baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
    }

}
